package com.cndw.kungfu.model.goods.domain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.ext.JsonProcesser;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.XmlProcesser;

/**
 * 系统奖励配置信息
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-11-10 下午10:26:46
 */
public class SysModeAwards {
	private static final Map<Integer, SysModeAwards> modeAwards = new HashMap<Integer, SysModeAwards>();
	private static final Map<Integer, Map<Integer, SysModeAwards>> depthAwards = new HashMap<Integer, Map<Integer, SysModeAwards>>();
	/**
	 * 奖励编号
	 */
	private int id;
	/**
	 * 大类id
	 */
	private int rootId;
	/**
	 * 子类编号
	 */
	private int typeId;
	/**
	 * 内部看
	 */
	private String use;
	/**
	 * 奖励内容描述
	 */
	private String title;
	/**
	 * 奖励内容配置
	 */
	private String subPops;

	private List<SubPops> subPopsList;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUse() {
		return use;
	}

	public void setUse(String use) {
		this.use = use;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSubPops() {
		return subPops;
	}

	public void setSubPops(String subPops) {
		this.subPops = subPops.trim();
	}

	public List<SubPops> getSubPopsList() {
		return subPopsList;
	}

	public void setSubPopsList(List<SubPops> subPopsList) {
		this.subPopsList = subPopsList;
	}

	public int getRootId() {
		return rootId;
	}

	public void setRootId(int rootId) {
		this.rootId = rootId;
	}

	public int getTypeId() {
		return typeId;
	}

	public void setTypeId(int typeId) {
		this.typeId = typeId;
	}

	public List<SubPops> makeAttachment() {
		List<SubPops> currentPops = new ArrayList<SubPops>();
		for (SubPops ep : subPopsList) {
			if (ep.getRantGoodsIds() == null) {
				currentPops.add(ep);
			} else {
				currentPops.add(ep.copyByRand());
			}
		}
		return currentPops;
	}

	static {
		reload();
	}

	public synchronized static void reload() {
		Map<Integer, SysModeAwards> modeAwardsTmpMap = new HashMap<Integer, SysModeAwards>();
		Map<Integer, Map<Integer, SysModeAwards>> depthAwardsTmpMap = new HashMap<Integer, Map<Integer, SysModeAwards>>();

		String fileName = ConstantsConfig.getLocaleResource() + "sysModeAwards.xml";
		String actFileName = ConstantsConfig.getLocaleResource() + "sysModeAwardsActivity.xml";
		List<SysModeAwards> tempList = XmlProcesser.processXml(SysModeAwards.class, fileName);
		tempList.addAll(XmlProcesser.processXml(SysModeAwards.class, actFileName));
		for (SysModeAwards e : tempList) {
			if (e.getSubPops() != null && e.getSubPops().length() > 1) {
				try {
					List<SubPops> pops = JsonProcesser.objectJsonArrayDecode(e.getSubPops(), SubPops.class);
					if (pops != null) {
						e.setSubPopsList(pops);
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
			if (depthAwardsTmpMap.containsKey(e.getRootId()) == false) {
				depthAwardsTmpMap.put(e.getRootId(), new HashMap<Integer, SysModeAwards>());
			}
			if (modeAwardsTmpMap.containsKey(e.getId()) || depthAwardsTmpMap.get(e.getRootId()).containsKey(e.getTypeId())) {
				LogUtil.getMain().error("err xxxx id" + e.getId() + " rootId:" + e.getRootId());
			}
			depthAwardsTmpMap.get(e.getRootId()).put(e.getTypeId(), e);
			modeAwardsTmpMap.put(e.getId(), e);
		}
		
		modeAwards.putAll(modeAwardsTmpMap);
		depthAwards.putAll(depthAwardsTmpMap);
		
	}

	public static SysModeAwards get(int awardsId) {
		return modeAwards.get(awardsId);
	}

	public static SysModeAwards get(int rootId, int typeId) {
		if (depthAwards.containsKey(rootId)) {
			return depthAwards.get(rootId).get(typeId);
		}
		return null;
	}

}
